package com.qianchang.microfinance.view.register;

import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.dm.library.utils.HtmlUtil;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.AgreementBean;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 协议
 */
public class AgreementActivity extends BaseActivity {

    @BindView(R.id.tv_product_introduction)
    WebView tvProductIntroduction;
    private HtmlUtil htmlUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_agreement);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void initView() {
        super.initView();
        WebSettings webSettings = tvProductIntroduction.getSettings();
        webSettings.setUseWideViewPort(true);//设置此属性，可任意比例缩放
//        webSettings.setLoadWithOverviewMode(true);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setSupportZoom(false);//支持缩放
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        //加载第三方页面时，我们没办法控制别人使用http还是https，如果我们服务器和对方使用不同的协议，就需要做这个处理了。
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        // 加载需要显示的网页
//        webView.addJavascriptInterface(new JavascriptInterface(), "classNameBeExposedInJs");
        tvProductIntroduction.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        htmlUtil = new HtmlUtil();
        htmlUtil.init(DMConstant.Config.BASE_URL);
    }

//        0 现金贷借款协议
//        1 消费贷借款协议
//        2 抵押贷借款协议
//        3 租房分期借款协议
//        4 学生贷借款协议
//        5 业主贷借款协议
//        6 用户服务协议
//        7 个人征信查询授权书
//        8 委托扣款授权书
//        9 数字证书服务协议
//        10 第三方风控服务授权书
//        100 芝麻信用授权页

    @Override
    protected void initData() {
        super.initData();
        Bundle bundle = getBundle();
        if(null != bundle){
            String agreementId = bundle.getString(ConstantAppInfo.AGREEMENT_ID);
            if(agreementId.equals("100")){
                String url = bundle.getString("url");
                tvProductIntroduction.loadUrl(url);
                setTitleName("芝麻信用授权");
                tvProductIntroduction.setWebChromeClient(new WebChromeClient());
                tvProductIntroduction.setWebViewClient(new WebViewClient(){
                    @Override
                    public void onPageStarted(WebView view, String url, Bitmap favicon) {
                        super.onPageStarted(view, url, favicon);
                        String strs = "webapp/qianpen/redirect/zhima";
                        String strUrl = url;
                        if(strUrl.contains(strs) == true){
                             AgreementActivity.this.finish();
                         }

                    }

                    @Override
                    public void onPageFinished(WebView view, String url) {
                        super.onPageFinished(view, url);
                         //http://19411b432a.iok.la:22643/user/qianpen/v/creditZhiMaH5CallBack.do?openIdInfo=eyJzdGF0ZSI6IjAwMTAxNTExNzY0NjI3Mzg5MTAxIiwib3BlbklkIjoiMjY4ODAwNjM5NzM2ODQ3NDE4MzI3MzQ3OTgyIn0=
                        String strUrl = url;

                    }
                });
                tvProductIntroduction.getSettings().setJavaScriptEnabled(true);
                tvProductIntroduction.loadUrl(url);



            }else {
                AgreementApi.getInfo(this, agreementId, new AgreementApi.CallBack() {
                    @Override
                    public void success(AgreementBean agreementBean) {
                        if ( agreementBean != null) {
                            String body = agreementBean.getAgreementText();
                            setTitleName(agreementBean.getAgreementName());
                            if (null != body) {
                                htmlUtil.getHead().append("<link rel='stylesheet' href='file:///android_asset/css/protocol.css' type='text/css'/>");

                                htmlUtil.setBody(new StringBuilder().append(body));
                                tvProductIntroduction.loadDataWithBaseURL(null,
                                        htmlUtil.createHtml(), "text/html", "utf-8", null);
                            }
                        }
                    }
                });
            }
        }
    }

}
